<?php

include_once "lib/config.php";

include_once "header.php";





if(isset($_POST['save'])&& $_POST['save']!="")

{

	$data= $_POST['App'];

    $data['last_modified_date']=time();

    $data['last_modified_by']=$_SESSION['u_id'];

    

    $filename="";

	

    $msg_error="";

    

	if($_FILES['app_photo']['name']!="")

    {

        include_once "thumb.php";

        if($_FILES['app_photo']['type']=="image/jpeg" || $_FILES['app_photo']['type']=="image/gif" || $_FILES['app_photo']['type']=="image/png" || $_FILES['app_photo']['type']=="image/jpg" || $_FILES['app_photo']['type']=="image/JPEG" || $_FILES['app_photo']['type']=="image/GIF" || $_FILES['app_photo']['type']=="image/PNG" || $_FILES['app_photo']['type']=="image/JPG")

        {

            list($width, $height) = getimagesize($_FILES['app_photo']["tmp_name"]);

            $thumbfolder_val="../uploads/apps/thumbs/";

            $folder_val="../uploads/apps";

            if(!file_exists($folder_val))

            {

				mkdir($folder_val,0777);

			}

            $folder_val=$folder_val."/";

            

            $newfilename="";

            

            $newfilename=$_FILES['app_photo']['name'];
            
            $tempfilepath=$folder_val.time()."_".$newfilename;

            $path=$folder_val.$newfilename;

            
            if(file_exists($path))

            {

				$filenamearr=explode(".",$newfilename);

                

                $newfilename="";

                

                for($i=0;$i<count($filenamearr)-1;$i++)

                {

                    $newfilename=$newfilename.$filenamearr[$i].".";

                }

                $newfilename=substr($newfilename,0,strlen($newfilename)-1);

                $newfilename=$newfilename."_".time().".".$filenamearr[count($filenamearr)-1];

                $path=$folder_val.$newfilename;

			}

            

            if($width>=166 && $height>=116)
            {
                if($_FILES['app_photo']['size']<=1048576)
                {
    
                    $move= move_uploaded_file($_FILES['app_photo']['tmp_name'], $tempfilepath);
    
                    if($move)
    
                    {
                        
                        createThumbsforProfiles_widthequal_Transparent($tempfilepath,$path,166,116);
                        
                        $thumbfolder_val=$thumbfolder_val.$newfilename;
    
                        createThumbsforProfiles_widthequal_Transparent($path,$thumbfolder_val,100,75);
    
                        $filename=$newfilename;
                        
                        unlink($tempfilepath);
                    }
    
                    
    
                }
    
                else
    
                {
    
                    
                    $msg_error="Image size should be less than or equal to 1MB";
    
                }
            }
        	else
            {
                $msg_error="Image width & height should be greater than 166px & 117px repectively.";
            }

        }

		else

		{

			$msg_error="Prfile image extension should be one of the (jpg,jpeg,png,gif)";

		}    

    }

    else

    {

		$msg_error="Image is not found!";

	}

    

    if(isset($_POST['app_id']) && $_POST['app_id']>0)

	{

	    if($_FILES['app_photo']['name']!="" && trim($filename)=="")

        {

            $msg_error="There is no record updated due to : ".$msg_error;

        }

        else

        {

            if($_FILES['app_photo']['name']!="")

            {

                if(file_exists("../uploads/apps/".$data["image_url"]))

                {

                    unlink("../uploads/apps/".$data["image_url"]);

                }

                if(file_exists("../uploads/apps/thumbs/".$data["image_url"]))

                {

                    unlink("../uploads/apps/thumbs/".$data["image_url"]);

                }

                $data["image_url"]=$filename;

            }

            

            

            $check_query=DB::updatecheckExistance("af_apps",array("app_name"=>$data['app_name'],"app_type"=>$data['app_type']),array("id"=>$_POST['app_id']));

        

    		if($check_query==0)

    		{

    		    $appdetail=DB::fetch_array(DB::query("select app_name,app_type from af_apps where id='".$_POST['app_id']."'"));

                $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Updated the ".$appdetail[0]['app_type']." app ".$appdetail[0]['app_name'],"logtime"=>time());

                DB::insertData("stafflog",$insertdataarr);

                

    		    DB::updateData("af_apps",$data,array("id"=>$_POST['app_id']));

    				

    			if(DB::$affectedrows>0)

    			{

    			    

    				$_SESSION['msg_success']="App updated successfully.";
                    
                    redirect('gamesapps.php');

    			}

    			else

    			{

    				$msg_error="There is no record updated.";

    			}

    		}

    		else

    		{

    			$msg_error="There is already one record exists for given app name in selected type.";

    		}

        }

	    

	}

	else

	{

	    if($_FILES['app_photo']['name']!="" && trim($filename)=="")

        {

            $msg_error="There is no record added due to : ".$msg_error;

        }

        else

        {

            $data["image_url"]=$filename;

    		$check_query=DB::checkExistance("af_apps",array("app_name"=>$data['app_name'],"app_type"=>$data['app_type']));

            

    		if($check_query==0)

    		{

    		    $data['added_date']=time();

                $data['added_by']=$_SESSION['u_id'];

                $data['last_modified_date']=time();

                $data['last_modified_by']=$_SESSION['u_id'];

                

    			$data['app_status']="Active";

    			

                $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Inserted the ".$data['app_name']." app ".$data['app_name'],"logtime"=>time());

                DB::insertData("stafflog",$insertdataarr);

                

                DB::insertData("af_apps",$data);

    			

                if(DB::$affectedrows>0)

    			{

    			 

    				$_SESSION['msg_success']="App added successfully.";
                    
    				redirect('gamesapps.php');

    			}

    			else

    			{

    				$msg_error="There is no record added.";

    			}

    			

    		}

    		else

    		{

    			$msg_error="There is already one record exists for given app name in selected app type.";

    		}

        }

	}

	

}

$dob=date("d/m/Y");

if(isset($_POST['app_id']) && $_POST['app_id']>0)

{

	$conditionarr=array(":id"=>$_POST['app_id']);

    $appinfo=DB::fetch_array(DB::query("select * from af_apps where id=:id",$conditionarr));

    

    if(isset($appinfo[0]))

    {

        $data=$appinfo[0];

    }

    else

    {

        $data=array();

    }

    

   

}



?>

  		<div class="container_12" id="main_div">

           <form action="" method="post" onsubmit="return validateaddeditapp();" enctype="multipart/form-data">

			<br>

			<div class="grid_12" >

		    	<?php

            	if(isset($msg_success) && $msg_success!="")

            	{

            	?>

                <div>

                    <span class="notification n-success"><?php echo $msg_success; ?></span>

                </div>

                <?php

                }

                if(isset($msg_error) && $msg_error!="")

                {

                ?>

                <span class="notification n-error"><?php echo $msg_error; ?></span>

                <?php

				}

                ?>

		        <div class="module" >

		        		

		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;App Details</span></h2>

		                

		             <div class="module-body" id="basic_body">

		                

		                  	<p>

		                        <label>App Name<span style="color: red;">*</span></label>

		                        <input type="text" class="input-short" style="width: 280px;" id="name" name="App[app_name]" value="<?php if(isset($data['app_name']) && $data['app_name']!=Null)echo $data['app_name']; ?>"/>

		                        

		                    </p>

		                    <p>

		                        <label>App Type<span style="color: red;">*</span></label>

		                        <select name="App[app_type]" id="app_type" class="input-short" style="width: 280px;">

                                    <option value=""> Select App Type </option>

                                    <option value="Web" <?php if(isset($data['app_type']) && $data['app_type']!=Null && $data['app_type']=="Web")echo "selected"; ?> >Web</option>

                                    <option value="Mobile" <?php if(isset($data['app_type']) && $data['app_type']!=Null && $data['app_type']=="Mobile")echo "selected"; ?> >Mobile</option>

                                </select>

		                    </p>

                            

                            <p>

		                        <label>App Image<span style="color: red;">*</span></label>

		                        <input type="file" name="app_photo" id="app_photo" /><span style="color: lightgray;">(Note: Image size should be less than or equal to 1MB ) </span><br /><br />

                                <input type="hidden" name="App[image_url]" id="image_url" value="<?php if(isset($data['image_url']) && $data['image_url']!=Null && $data['image_url']!="")echo $data['image_url']; ?>" />

                                <?php if(isset($data['image_url']) && $data['image_url']!=Null && $data['image_url']!="")

                                {

                                    ?>

                                    <img src="../uploads/apps/thumbs/<?php echo $data['image_url']; ?>"/>  

                                    <?php

                                }

                                ?>

		                    </p>

                            

                            <p>

		                        <label>App small description<span style="color: red;">*</span></label>

		                        <input type="text" name="App[small_description]" id="smalldescription" class="input-short" value="<?php if(isset($data['small_description']) && $data['small_description']!=Null && $data['small_description']!=""){ echo $data["small_description"]; } ?>" maxlength="50" style="width: 280px;" />

                                    

		                    </p>

                            

		             </div> <!-- End .module-body -->

		

		        </div>  <!-- End .module -->

				

		    </div>

		    

		    <div class="grid_12">

		    <fieldset>

		    	&nbsp;

		    	<input type="hidden" id="app_id" name="app_id" value="<?php if(isset($_POST['app_id'])){ echo $_POST['app_id']; } ?>">

		        <input class="submit-green" type="submit" name="save" value="Submit" /> 

		        <input class="submit-gray" type="button" value="Back" onclick="window.location='gamesapps.php'" />

		    </fieldset>

		    </div>

		    <div style="clear:both;"></div>

		 </form>    

        </div> <!-- End .container_12 -->

		

<?php include_once("footer.php"); ?>           

<?php

if(isset($msg_success) && $msg_success!="")

{

	$notymessage=$msg_success;

	$msgtype="success";

}

else if(isset($msg_error) && $msg_error!="")

{

	$msgtype="error";

	$notymessage=$msg_error;

}

else

{

	$msgtype="";

}

if($msgtype!="")

{

?>        

<script language="javascript">

	$(document).ready(function(){

		

		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');

		noty(options);

		

	});

</script>  

<?php

}

?>

<script>



function validateaddeditapp()

{

	

	if(trim(document.getElementById("name").value)=="")

	{

		alertify.alert("App Name field is required!");

		document.getElementById("name").focus();

		return false;

	}

	else if(trim(document.getElementById("app_type").value)=="")

	{

		alertify.alert("App Type field is required!");

		document.getElementById("app_type").focus();

		return false;

	}

    else if(trim(document.getElementById("app_photo").value)=="" && trim(document.getElementById('image_url').value)=="")

	{

		alertify.alert("App image field is required!");

		document.getElementById("app_photo").focus();

		return false;

	}

    else if(trim(document.getElementById("smalldescription").value)=="")

	{

		alertify.alert("App small description of maximum 50 characters is required!");

		document.getElementById("smalldescription").focus();

		return false;

	}

}

</script>       